load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")

licenses(["restricted"])  # GPL v3

cc_library(
    name = "readline",
    srcs = glob(
        ["*.c"],
        exclude = ["*_keymap.c"],
    ) + [
        "readline/config.h",
    ],
    hdrs = glob(["*.h"]) + [
        "emacs_keymap.c",
        "vi_keymap.c",
    ],
    copts = [
        "-w",
        "-DHAVE_CONFIG_H",
        "-DRL_LIBRARY_VERSION='\"8.2\"'",
    ],
    defines = [
        "READLINE_HAVE_CONFIG_H",
    ],
    includes = [
        "readline",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":prefixed_readline_header",
        "@ncurses",
    ],
)

cc_library(
    name = "prefixed_readline_header",
    hdrs = glob(["*.h"]) + [
        "emacs_keymap.c",
        "readline/config.h",
        "vi_keymap.c",
    ],
    include_prefix = "readline",
)

expand_template(
    name = "config_h",
    out = "readline/config.h",
    substitutions = {
        "#undef CTYPE_NON_ASCII": "/* #undef CTYPE_NON_ASCII */",
        "#undef FIONREAD_IN_SYS_IOCTL": "#define FIONREAD_IN_SYS_IOCTL 1",
        "#undef HAVE_BSD_SIGNALS": "/* #undef HAVE_BSD_SIGNALS */",
        "#undef HAVE_CHOWN": "#define HAVE_CHOWN 1",
        "#undef HAVE_DIRENT_H": "#define HAVE_DIRENT_H 1",
        "#undef HAVE_FCNTL_H": "#define HAVE_FCNTL_H 1",
        "#undef HAVE_FNMATCH": "#define HAVE_FNMATCH 1",
        "#undef HAVE_GETPWENT": "#define HAVE_GETPWENT 1",
        "#undef HAVE_GETPWNAM": "#define HAVE_GETPWNAM 1",
        "#undef HAVE_GETPWUID": "#define HAVE_GETPWUID 1",
        "#undef HAVE_GETPW_DECLS": "#define HAVE_GETPW_DECLS 1",
        "#undef HAVE_GETTIMEOFDAY": "#define HAVE_GETTIMEOFDAY 1",
        "#undef HAVE_ISASCII": "#define HAVE_ISASCII 1",
        "#undef HAVE_ISWCTYPE": "#define HAVE_ISWCTYPE 1",
        "#undef HAVE_ISWLOWER": "#define HAVE_ISWLOWER 1",
        "#undef HAVE_ISWUPPER": "#define HAVE_ISWUPPER 1",
        "#undef HAVE_ISXDIGIT": "#define HAVE_ISXDIGIT 1",
        "#undef HAVE_KILL": "#define HAVE_KILL 1",
        "#undef HAVE_LANGINFO_CODESET": "#define HAVE_LANGINFO_CODESET 1",
        "#undef HAVE_LANGINFO_H": "#define HAVE_LANGINFO_H 1",
        "#undef HAVE_LIBAUDIT_H": "/* #undef HAVE_LIBAUDIT_H */",
        "#undef HAVE_LIMITS_H": "#define HAVE_LIMITS_H 1",
        "#undef HAVE_LOCALE_H": "#define HAVE_LOCALE_H 1",
        "#undef HAVE_LSTAT": "#define HAVE_LSTAT 1",
        "#undef HAVE_MBRLEN": "#define HAVE_MBRLEN 1",
        "#undef HAVE_MBRTOWC": "#define HAVE_MBRTOWC 1",
        "#undef HAVE_MBSRTOWCS": "#define HAVE_MBSRTOWCS 1",
        "#undef HAVE_MBSTATE_T": "#define HAVE_MBSTATE_T 1",
        "#undef HAVE_MEMMOVE": "#define HAVE_MEMMOVE 1",
        "#undef HAVE_MEMORY_H": "#define HAVE_MEMORY_H 1",
        "#undef HAVE_NDIR_H": "/* #undef HAVE_NDIR_H */",
        "#undef HAVE_POSIX_SIGNALS": "#define HAVE_POSIX_SIGNALS 1",
        "#undef HAVE_POSIX_SIGSETJMP": "#define HAVE_POSIX_SIGSETJMP 1",
        "#undef HAVE_PSELECT": "#define HAVE_PSELECT 1",
        "#undef HAVE_PUTENV": "#define HAVE_PUTENV 1",
        "#undef HAVE_PWD_H": "#define HAVE_PWD_H 1",
        "#undef HAVE_READLINK": "#define HAVE_READLINK 1",
        "#undef HAVE_SELECT": "#define HAVE_SELECT 1",
        "#undef HAVE_SETENV": "#define HAVE_SETENV 1",
        "#undef HAVE_SETITIMER": "#define HAVE_SETITIMER 1",
        "#undef HAVE_SETLOCALE": "#define HAVE_SETLOCALE 1",
        "#undef HAVE_STDARG_H": "#define HAVE_STDARG_H 1",
        "#undef HAVE_STDBOOL_H": "#define HAVE_STDBOOL_H 1",
        "#undef HAVE_STDLIB_H": "#define HAVE_STDLIB_H 1",
        "#undef HAVE_STRCASECMP": "#define HAVE_STRCASECMP 1",
        "#undef HAVE_STRCOLL": "#define HAVE_STRCOLL 1",
        "#undef HAVE_STRINGS_H": "#define HAVE_STRINGS_H 1",
        "#undef HAVE_STRING_H": "#define HAVE_STRING_H 1",
        "#undef HAVE_STRPBRK": "#define HAVE_STRPBRK 1",
        "#undef HAVE_STRUCT_DIRENT_D_FILENO": "#define HAVE_STRUCT_DIRENT_D_FILENO 1",
        "#undef HAVE_STRUCT_DIRENT_D_INO": "#define HAVE_STRUCT_DIRENT_D_INO 1",
        "#undef HAVE_STRUCT_DIRENT_D_NAMLEN": "/* #undef HAVE_STRUCT_DIRENT_D_NAMLEN */",
        "#undef HAVE_SYSCONF": "#define HAVE_SYSCONF 1",
        "#undef HAVE_SYS_DIR_H": "/* #undef HAVE_SYS_DIR_H */",
        "#undef HAVE_SYS_FILE_H": "#define HAVE_SYS_FILE_H 1",
        "#undef HAVE_SYS_IOCTL_H": "#define HAVE_SYS_IOCTL_H 1",
        "#undef HAVE_SYS_NDIR_H": "/* #undef HAVE_SYS_NDIR_H */",
        "#undef HAVE_SYS_PTEM_H": "/* #undef HAVE_SYS_PTEM_H */",
        "#undef HAVE_SYS_PTE_H": "/* #undef HAVE_SYS_PTE_H */",
        "#undef HAVE_SYS_SELECT_H": "#define HAVE_SYS_SELECT_H 1",
        "#undef HAVE_SYS_STREAM_H": "/* #undef HAVE_SYS_STREAM_H */",
        "#undef HAVE_SYS_TIME_H": "#define HAVE_SYS_TIME_H 1",
        "#undef HAVE_TCGETATTR": "#define HAVE_TCGETATTR 1",
        "#undef HAVE_TERMIOS_H": "#define HAVE_TERMIOS_H 1",
        "#undef HAVE_TIMEVAL": "#define HAVE_TIMEVAL 1",
        "#undef HAVE_TOWLOWER": "#define HAVE_TOWLOWER 1",
        "#undef HAVE_TOWUPPER": "#define HAVE_TOWUPPER 1",
        "#undef HAVE_UNISTD_H": "#define HAVE_UNISTD_H 1",
        "#undef HAVE_USG_SIGHOLD": "/* #undef HAVE_USG_SIGHOLD */",
        "#undef HAVE_VARARGS_H": "/* #undef HAVE_VARARGS_H */",
        "#undef HAVE_VSNPRINTF": "#define HAVE_VSNPRINTF 1",
        "#undef HAVE_WCHAR_H": "#define HAVE_WCHAR_H 1",
        "#undef HAVE_WCHAR_T": "#define HAVE_WCHAR_T 1",
        "#undef HAVE_WCRTOMB": "#define HAVE_WCRTOMB 1",
        "#undef HAVE_WCSCOLL": "#define HAVE_WCSCOLL 1",
        "#undef HAVE_WCTYPE_H": "#define HAVE_WCTYPE_H 1",
        "#undef HAVE_WCTYPE_T": "#define HAVE_WCTYPE_T 1",
        "#undef HAVE_WCWIDTH": "#define HAVE_WCWIDTH 1",
        "#undef HAVE_WINT_T": "#define HAVE_WINT_T 1",
        "#undef MUST_REINSTALL_SIGHANDLERS": "/* #undef MUST_REINSTALL_SIGHANDLERS */",
        "#undef NO_MULTIBYTE_SUPPORT": "/* #undef NO_MULTIBYTE_SUPPORT */",
        "#undef PROTOTYPES": "#define PROTOTYPES 1",
        "#undef RETSIGTYPE": "#define RETSIGTYPE void",
        "#undef SPEED_T_IN_SYS_TYPES": "/* #undef SPEED_T_IN_SYS_TYPES */",
        "#undef STAT_MACROS_BROKEN": "/* #undef STAT_MACROS_BROKEN */",
        "#undef STDC_HEADERS": "#define STDC_HEADERS 1",
        "#undef STRCOLL_BROKEN": "/* #undef STRCOLL_BROKEN */",
        "#undef STRUCT_WINSIZE_IN_SYS_IOCTL": "#define STRUCT_WINSIZE_IN_SYS_IOCTL 1",
        "#undef STRUCT_WINSIZE_IN_TERMIOS": "/* #undef STRUCT_WINSIZE_IN_TERMIOS */",
        "#undef VOID_SIGHANDLER": "#define VOID_SIGHANDLER 1",
        "#undef _ALL_SOURCE": "#define _ALL_SOURCE 1",
        "#undef _FILE_OFFSET_BITS": "/* #undef _FILE_OFFSET_BITS */",
        "#undef _GNU_SOURCE": "#define _GNU_SOURCE 1",
        "#undef _MINIX": "/* #undef _MINIX */",
        "#undef _POSIX_1_SOURCE": "/* #undef _POSIX_1_SOURCE */",
        "#undef _POSIX_PTHREAD_SEMANTICS": "#define _POSIX_PTHREAD_SEMANTICS 1",
        "#undef _POSIX_SOURCE": "/* #undef _POSIX_SOURCE */",
        "#undef _TANDEM_SOURCE": "#define _TANDEM_SOURCE 1",
        "#undef __EXTENSIONS__": "#define __EXTENSIONS__ 1",
        "#undef __PROTOTYPES": "#define __PROTOTYPES 1",
        "#undef const": "/* #undef const */",
        "#undef inline": "/* #undef inline */",
        "#undef sig_atomic_t": "/* #undef sig_atomic_t */",
        "#undef size_t": "/* #undef size_t */",
        "#undef ssize_t": "/* #undef ssize_t */",
        "#undef volatile": "/* #undef volatile */",
    } | {
        # These are separated to avoid replacement issues from sorted dicts.
        "#undef HAVE_FCNTL": "#define HAVE_FCNTL 1",
        "#undef HAVE_WCTYPE": "#define HAVE_WCTYPE 1",
    } | select({
        "@platforms//os:macos": {
            "#undef GWINSZ_IN_SYS_IOCTL": "/* #undef GWINSZ_IN_SYS_IOCTL */",
            "#undef HAVE_DECL_AUDIT_USER_TTY": "#define HAVE_DECL_AUDIT_USER_TTY 0",
            "#undef HAVE_TERMCAP_H": "#define HAVE_TERMCAP_H 1",
            "#undef HAVE_TERMIO_H": "/* #undef HAVE_TERMIO_H */",
            "#undef TIOCSTAT_IN_SYS_IOCTL": "#define TIOCSTAT_IN_SYS_IOCTL 1",
            "#undef WCWIDTH_BROKEN": "/* #undef WCWIDTH_BROKEN */",
            "#undef __CHAR_UNSIGNED__": "/* #undef __CHAR_UNSIGNED__ */",
        },
        "//conditions:default": {
            "#undef CTYPE_NON_ASCII": "#define CTYPE_NON_ASCII 1",
            "#undef GWINSZ_IN_SYS_IOCTL": "#define GWINSZ_IN_SYS_IOCTL 1",
            "#undef HAVE_DECL_AUDIT_USER_TTY": "#define HAVE_DECL_AUDIT_USER_TTY 1",
            "#undef HAVE_TERMCAP_H": "/* #undef HAVE_TERMCAP_H */",
            "#undef HAVE_TERMIO_H": "#define HAVE_TERMIO_H 1",
            "#undef TIOCSTAT_IN_SYS_IOCTgL": "/* #undef TIOCSTAT_IN_SYS_IOCTL */",
            "#undef WCWIDTH_BROKEN": "#define WCWIDTH_BROKEN 1",
            "#undef __CHAR_UNSIGNED__": "#define __CHAR_UNSIGNED__ 1",
        },
    }),
    template = "config.h.in",
)

# Compile an example binary to show we're able to link.
cc_binary(
    name = "examples/rltest",
    srcs = ["examples/rltest.c"],
    copts = [
        "-DHAVE_CONFIG_H",
    ],
    deps = [":readline"],
)
